﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmFeeBaseMgr : Form
    {
        public FrmFeeBaseMgr()
        {
            InitializeComponent();
        }

        private void FrmFeeBaseMgr_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            // TODO: 这行代码将数据加载到表“cjwyglDataSet_feebase.CJ_FEE_BASE”中。您可以根据需要移动或删除它。
            this.cJ_FEE_BASETableAdapter.Fill(this.cjwyglDataSet_feebase.CJ_FEE_BASE);


        }

        private void LoadInfo(int id)
        {
            using (DataBase db = new DataBase())
            {
                string sql = "select UNIT_NAME,UNIT_PRICE,COMMENT FROM CJ_FEE_BASE WHERE ID=" + id;
                DataTable dt = db.ExecuteDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    textBox_mingmu.Text = dt.Rows[0][0].ToString();
                    textBox_unitprice.Text = dt.Rows[0][1].ToString();
                    textBox_comment.Text = dt.Rows[0][2].ToString();
                }
            }
        }
        private void button_add_Click(object sender, EventArgs e)
        {
            int ret = AddNew();
            if (ret == -1)
            {
                MessageBox.Show("更新成功");
            }
            else if (ret == 1)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            LoadData();
            
        }
        private bool CheckIfExist(string mingmu)
        {
            using (DataBase db = new DataBase())
            {
                string sql = "select ID from CJ_FEE_BASE where UNIT_NAME='"+mingmu+"'";
                return (db.ExecuteDataTable(sql).Rows.Count > 0);
            }
        }
        /// <summary>
        /// Add new record
        /// </summary>
        /// <returns>1:successful 0:failed -1:change to modifyold</returns>
        private int AddNew()
        {
            string mingmu = textBox_mingmu.Text;
            if (CheckIfExist(mingmu))
            {
                DialogResult dr = MessageBox.Show("该项已经存在，确认修改请按确认，放弃按取消！",
                    "该项记录已经存在", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    ModifyOld(mingmu);
                    return -1;
                }
                return 0;
                
            }
            double unitprice = 0.0;
            double.TryParse(textBox_unitprice.Text, out unitprice);
            if (mingmu.Trim() == "" || textBox_unitprice.Text.Trim()=="")
            {
                MessageBox.Show("名目与单价不能为空");
                return 0;
            }
            string comment = textBox_comment.Text;
            string sql = "INSERT INTO [CJ_FEE_BASE]( "+
                "[UNIT_NAME],[UNIT_PRICE],[COMMENT]) "+
                "VALUES(@UNIT_NAME,@UNIT_PRICE,@COMMENT)";
            using (DataBase db = new DataBase())
            {
                db.AddParameter("UNIT_NAME", mingmu);
                db.AddParameter("UNIT_PRICE", unitprice);
                db.AddParameter("COMMENT", comment);
                return db.ExecuteNonQuery(sql);
            }
            
        }
        private int ModifyOld(int id)
        {
            string mingmu = textBox_mingmu.Text;
            double unitprice = 0.0;
            double.TryParse(textBox_unitprice.Text, out unitprice);
            if (mingmu.Trim() == "" || textBox_unitprice.Text.Trim() == "")
            {
                MessageBox.Show("名目与单价不能为空");
                return 0;
            }
            string comment = textBox_comment.Text;
            string sql = "UPDATE [CJ_FEE_BASE] "+
                "   SET [UNIT_NAME] = @UNIT_NAME "+
                "      ,[UNIT_PRICE] = @UNIT_PRICE "+
                "      ,[COMMENT] = @COMMENT "+
                " WHERE ID=@ID";
            using (DataBase db = new DataBase())
            {
                db.AddParameter("UNIT_NAME", mingmu);
                db.AddParameter("UNIT_PRICE", unitprice);
                db.AddParameter("COMMENT", comment);
                db.AddParameter("ID", id);
                return db.ExecuteNonQuery(sql);
            }
        }
        private int ModifyOld(string mingmu)
        {
            double unitprice = 0.0;
            double.TryParse(textBox_unitprice.Text, out unitprice);
            if (mingmu.Trim() == "" || textBox_unitprice.Text.Trim() == "")
            {
                MessageBox.Show("名目与单价不能为空");
                return 0;
            }
            string comment = textBox_comment.Text;
            string sql = "UPDATE [CJ_FEE_BASE] " +
                "   SET [UNIT_PRICE] = @UNIT_PRICE " +
                "      ,[COMMENT] = @COMMENT " +
                " WHERE UNIT_NAME=@UNIT_NAME";
            using (DataBase db = new DataBase())
            {
                db.AddParameter("UNIT_NAME", mingmu);
                db.AddParameter("UNIT_PRICE", unitprice);
                db.AddParameter("COMMENT", comment);
                return db.ExecuteNonQuery(sql);
            }
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                int ret = ModifyOld(id);
                if (ret == 1)
                {
                    MessageBox.Show("更新成功");
                }
                else
                {
                    MessageBox.Show("更新失败");
                }

            }
            LoadData();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                LoadInfo(id);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                DialogResult dr = MessageBox.Show("确认要删除该行吗？", "确认要删除该行吗？",
                    MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                using (DataBase db = new DataBase())
                {
                    string sql = "delete from CJ_FEE_BASE WHERE ID=" + id;
                    MessageBox.Show("已经删除" + db.ExecuteNonQuery(sql) + "行");
                    
                }
            }
            else
            {
                MessageBox.Show("未选中任何行");
            }
            LoadData();
        }


    }
}
